#include "command.h"

void handler_ls(char *user_name, data *msg, int net_fd, MYSQL *mysql){
    
    // 执行 "ls" 命令的sql语句
    char sql_buf[2048] = {0};
    sprintf(sql_buf, "SELECT file_name FROM file WHERE father_id = \
                        (SELECT file_id FROM file WHERE user_id = \
                        (SELECT user_id FROM user WHERE user_name = '%s') \
                        AND file_path = '%s' AND is_deleted = 0) AND is_deleted = 0",
            user_name, msg->args);
    mysql_query(mysql, sql_buf);
    MYSQL_RES *result = mysql_store_result(mysql);

    // 读取sql执行结果
    char buf[1024] = {0};
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result))){
        for (int i = 0; i < mysql_num_fields(result); i++){
            strncat(buf, row[i], strlen(row[i]));
            strncat(buf, " \t ", 4);
        }
    }
    mysql_free_result(result);

    // 将 "ls" 结果发送给客户端
    send(net_fd, buf, sizeof(buf), 0);

}